// pages/feedback/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      {
        id: 0,
        value: "体验问题",
        isActive: true
      },
      {
        id: 1,
        value: "商品、商家投诉",
        isActive: false
      }
    ],

    chooseImgs: [],
    textVal: ""
  },
  UploadImgs: [],
  handletabsItemChange(e) {
    let { index } = e.detail
    let { tabs } = this.data
    tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false)

    this.setData({
      tabs
    })

  },
  handleChooseImg() {
    wx.chooseImage({
      count: 9,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (result) => {
        this.setData({
          chooseImgs: [...this.data.chooseImgs, ...result.tempFilePaths]
        })

      }

    });

  },
  handleRemoveImg(e) {
    const { index } = e.currentTarget.dataset
    let { chooseImgs } = this.data

    chooseImgs.splice(index, 1)

    this.setData({
      chooseImgs
    })

  },
  handleTextInp(e) {
    this.setData({
      textVal: e.detail.value
    })
  },
  handleFormSubmit() {
    const { textVal, chooseImgs } = this.data
    if (!textVal.trim()) {
      wx.showToast({
        title: '输入不合法',
        icon: 'none',
        mask: true,
      });
      return
    }
    wx.showLoading({
      title: '正在上传中',
      mask: true
    });

    if (chooseImgs.length !== 0) {
      chooseImgs.forEach((v, i) => {
        wx.uploadFile({
          url: 'https://apis.yum6.cn/api/5bd7f16aa33d7',
          filePath: v,
          name: 'file',
          formData: {},
          success: (result) => {
            let url = JSON.parse(result.data).data
            this.UploadImgs.push(url)

            wx.hideLoading();

            if (i === chooseImgs.length - 1) {
              console.log("提交到后台接口");

              this.setData({
                textVal: "",
                chooseImgs: []
              })

              wx.navigateBack({
                delta: 1
              });

            }
          }

        });

      })
    } else {
      wx.hideLoading();
      console.log("只是提交了文本");
      wx.navigateBack({
        delta: 1
      });

    }

  }

})